﻿#include "UAddDevice.h"
#include <QTextStream>
#include <QDir>
#include "Global.h"
UAddDevice::UAddDevice(CBaseNode* pNode, CDeviceManager *pDeviceManager, QWidget *parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	m_pParent = pNode;
	m_pDeviceManager = pDeviceManager;
	initUi();
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());

	// 设置标题栏颜色（RGB）
	setNativeTitleBarColor(hwnd, RGB(90, 102, 131));  // 深灰色
}

UAddDevice::~UAddDevice()
{
}

void UAddDevice::initUi()
{
	connect(ui.comboBox_2, SIGNAL(currentIndexChanged(int)), this, SLOT(slotIndexChangedName(int)));
	QVector<QString>vecFileName= m_pDeviceManager->getDllListFromPath();
	for (QMap<QString,QString>::iterator it= g_mapDllName.begin();it!= g_mapDllName.end();it++)
	{
		ui.comboBox_2->addItem(it.key());
	}
	QString sNodeName = "#" + QString::number(m_pParent->getChildren().size() + 1) + ui.comboBox_2->currentText();
	ui.lineEdit->setText(sNodeName);
	connect(ui.pushButton, SIGNAL(clicked()), this, SLOT(slotAddDev()));
	connect(ui.pushButton_2, SIGNAL(clicked()), this, SLOT(slotClose()));
}

CBaseDevice * UAddDevice::getDevice()
{
	return m_pCurrDevice;
}

void UAddDevice::slotAddDev()
{
	QString sName = ui.lineEdit->text();
	QString sDllName = g_mapDllName[ui.comboBox_2->currentText()];
	int nType = ui.comboBox->currentIndex();
	QString sConfig = sDllName + "-" + QString::number(nType);
	QVector<CBaseDevice*>vecBaseDevice = m_pDeviceManager->createDevices(sName, sConfig, m_pParent);
	if (vecBaseDevice.size() > 0)
	{
		QString sTech = ui.com_AlarmGY->currentText();
		QString sFault = ui.com_AlarmGZ->currentText();
		for (int i = 0; i < vecBaseDevice.size(); i++)
		{
			vecBaseDevice[i]->setTechAlarmArea(sTech);
			vecBaseDevice[i]->setFaultAlarmArea(sFault);
		}
		emit deviceAdded(vecBaseDevice[vecBaseDevice.size() - 1]);              // 发送信号
	}
	close();
}


void UAddDevice::slotClose()
{
	close();
}
void UAddDevice::slotIndexChangedName(int nIndex)
{
	QVector<QString>vevModeName;
	QString sNodeName ="#" + QString::number(m_pParent->getChildren().size() + 1) + ui.comboBox_2->currentText();
	ui.lineEdit->setText(sNodeName);
	QString sDllName = g_mapDllName[ui.comboBox_2->currentText()];
	vevModeName= m_pDeviceManager->getModelList(sDllName);
	ui.comboBox->clear();
	for (const QString& name : vevModeName)
	{
		ui.comboBox->addItem(name);
	}
}